/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public abstract class CardGameScene extends Scene {

    public static final int NUMBER_CARDS = 53;
    public static final int DISTANCE_HORIZONTAL_CARD = 15;
    protected int delta;
    protected int timeForThink;
    protected int handCurseurIndex;
    protected int numberCardRemains;
    private int pointerPressX;
    protected int poiterPressY;
    protected int maxPlayer;
    protected int myViewId;
    protected int playerId;
    protected int gameTurn;
    protected int numberPlayers;
    protected int cardHeight;
    protected int cardWidth;
    protected int topCardInHandInitValue;
    protected int topCardInHand;
    protected int firstTurn;
    protected long lastTime;
    protected int avatarHeight;
    protected int avatarWidth;
    protected boolean isViewer;
    protected boolean isJoinedGame;
    protected boolean isFinishGame;
    private boolean isPointerDrag;
    //-------------------------------------------------------------------
    protected Image handCurSeurImage;
    protected Image avatarImage;
    //--------------------------------------------------------------------------
    protected int[] positionX;
    protected int[] positionY;
    protected Card[] cardInHands;
    protected CustomTextBox chatBox;
    private ShortShowBox[] messengerBoxs;
    private FlyString[] moneyFlyStrings;
    protected boolean isShowJoin;

    public CardGameScene(MainCanvas parent) {
        super(parent);
        delta = -5;
        bottomBarHeight = 20;
        subMenuBottom = canvasHeight - bottomBarHeight;
        initObject();
    }

    private void initObject() {
        try {
            avatarImage = Utils.loadImage("new_avatar.png");
            handCurSeurImage = library.getHandCurSeur();
            chatBox = library.getChatBox(keyPad);
            messengerBoxs = library.getMessengerBox();
            moneyFlyStrings = new FlyString[4];
            for (int i = 0; i < 4; i++) {
                moneyFlyStrings[i] = new FlyString(FlyString.FLY_VERTICAL, PiPoDesigner.COLOR_YELOW);
            }
            initPosition();
        } catch (IOException ex) {
        }
    }

    private void initPosition() {
        avatarWidth = avatarImage.getWidth() / 11;
        avatarHeight = avatarImage.getHeight() / 2;
        positionX = new int[4];
        positionX[0] = positionX[2] = canvasWidth / 2 - avatarWidth / 2;
        positionX[3] = leftScreen + 10;
        positionX[1] = canvasWidth - 10 - avatarWidth;
        positionY = new int[4];
        positionY[0] = canvasHeight - avatarHeight - 10;
        positionY[1] = positionY[3] = canvasHeight / 2 - avatarHeight / 2;
        positionY[2] = topScreen + 10;
    }

    public void setListSubMenu() {
        subMenu.setList(new String[]{"Rời bàn", "Loa"});
    }

    public void dispose() {
        avatarImage = null;
        handCurSeurImage = null;
        //--------------------------------------------------------------------------
        positionX = null;
        positionY = null;
        cardInHands = null;
        chatBox = null;
        messengerBoxs = null;
        moneyFlyStrings = null;
        super.dispose();
    }

    //----------------------------------------------------------
    protected abstract void doWhenTimeExpired();

    public void clockCount() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastTime > 1000) {
            timeForThink--;
            lastTime = currentTime;
            doWhenTimeExpired();
            parent.repaint();
        }
    }

    protected void switchMessengerBox(int posId, String text) {
        switch (posId) {
            case 0:
                messengerBoxs[0].setText(text);
                messengerBoxs[0].setPosition(canvasWidth / 2 - messengerBoxs[0].getBoxWidth() / 2, positionY[0]);
                break;
            case 1:
                messengerBoxs[1].setText(text);
                messengerBoxs[1].setPosition(canvasWidth - 10 - messengerBoxs[1].getBoxWidth(), positionY[1]);
                break;
            case 2:
                messengerBoxs[2].setText(text);
                messengerBoxs[2].setPosition(canvasWidth / 2 - messengerBoxs[2].getBoxWidth() / 2, positionY[2]);
                break;
            case 3:
                messengerBoxs[3].setText(text);
                messengerBoxs[3].setPosition(positionX[3], positionY[3]);
                break;
        }
        messengerBoxs[posId].show(5000);
    }

    public void switchMoneyChange(int posId, String text) {
        switch (posId) {
            case 0:
                moneyFlyStrings[0].setText(text, 1, positionX[0], positionY[0]);
                break;
            case 1:
                moneyFlyStrings[1].setText(text, 1, positionX[1], positionY[1]);
                break;
            case 2:
                moneyFlyStrings[2].setText(text, 1, positionX[2], positionY[2]);
                break;
            case 3:
                moneyFlyStrings[3].setText(text, 1, positionX[3], positionY[3]);
                break;
        }
        moneyFlyStrings[posId].show(5000);
    }
//------------------------------------------------------------------------------

    public void hideMessage() {
        for (int i = 0; i < maxPlayer; i++) {
            if (messengerBoxs[i] != null) {
                messengerBoxs[i].isShow = false;
            }
        }
        parent.repaint();
    }

    public void doLoop() {
        super.doLoop();
        doCardGameLoop();
        if (isShowChatBox && countFrame % 4 == 0) {
            chatBox.run();
            parent.repaint();
        }
        for (int i = 0; i < 4; i++) {
            if (messengerBoxs[i].isShow) {
                messengerBoxs[i].run();
                parent.repaint();
            }
        }
        for (int i = 0; i < 4; i++) {
            if (moneyFlyStrings[i].isShow) {
                moneyFlyStrings[i].run();
                parent.repaint();
            }
        }
        if (countFrame % 10 == 0) {
            delta = -delta;
            parent.repaint();
        }
        if (!isFinishGame) {
            clockCount();
        }
    }

    public void setCardPosition() {
        int cardInHandWidth = DISTANCE_HORIZONTAL_CARD * (numberCardRemains - 1) + cardWidth;
        int leftCardInHand = canvasWidth / 2 - cardInHandWidth / 2;
        topCardInHand = topCardInHandInitValue;
        for (int i = 0; i < numberCardRemains; i++) {
            cardInHands[i].setPosition(leftCardInHand + DISTANCE_HORIZONTAL_CARD * i, topCardInHand);
            cardInHands[i].setState(Card.STATE_NORMAL);
        }
        parent.repaint();
    }

    abstract void doCardGameLoop();
//--------------------------------------------------------------------------------

    public void paintComponents(Graphics g) {
        paintCardGame(g);
        paintMessageChat(g);
        if (isShowChatBox) {
            chatBox.paint(g, leftScreen + 10, canvasHeight - 45, canvasWidth - 20, 20);
        }
        paintMoneyChange(g);
    }

    private void paintMessageChat(Graphics g) {
        messengerBoxs[0].paint(g);
        messengerBoxs[1].paint(g);
        messengerBoxs[2].paint(g);
        messengerBoxs[3].paint(g);
    }

    protected abstract void paintCardGame(Graphics g);

    private void paintMoneyChange(Graphics g) {
        moneyFlyStrings[0].paint(g);
        moneyFlyStrings[1].paint(g);
        moneyFlyStrings[2].paint(g);
        moneyFlyStrings[3].paint(g);
    }
//---------------------------------------------------------------------------

    protected void doSubMenu() {
        isShowSubMenu = false;
        subMenu.setVisible(false);
        Request.exitTable();
        isLoading = true;
        return;
    }

    protected void doKeyCenterRemain() {
        Debug.d("do key center:" + isShowJoin);
        if (isShowChatBox && !isViewer) {
            String messageText = chatBox.getText();
            if (messageText.length() == 0) {
                isShowChatBox = false;
                parent.repaint();
                return;
            }
            if (messageText.length() > 25) {
                dialogString = "Số kí tự chat không vượt quá 25";
                parent.repaint();
                return;
            }

            switchMessengerBox(0, messageText);
            isShowChatBox = false;
            Request.chat(chatBox.getText());
            chatBox.setText("");
            parent.repaint();
            return;
        }
        if (isViewer && !isJoinedGame && isShowJoin) {
            Debug.d("request join ");
            isLoading = true;
            Request.joinGame(gameData.getMyName());
            parent.repaint();
            return;
        }
        doGameKeyCenter();
    }

    abstract void doGameKeyCenter();

    public void doKeyLeft() {
        if (isViewer) {
            return;
        }
        boolean isSwapCard = false;
        if (handCurseurIndex < 0) {
            handCurseurIndex = numberCardRemains - 1;
        }
        if (cardInHands[handCurseurIndex].getState() == Card.STATE_LOW) {
            isSwapCard = true;
        }
        handCurseurIndex--;
        if (handCurseurIndex < 0) {
            handCurseurIndex = numberCardRemains - 1;
        }
        if (isSwapCard) {
            swapCard(handCurseurIndex + 1, handCurseurIndex);
        }
        parent.repaint();
    }

    public void swapCard(int srcIndex, int desIndex) {
        // Debug.d("swap card");
        if (srcIndex < 0) {
            srcIndex = numberCardRemains - 1;
        } else if (srcIndex == numberCardRemains) {
            srcIndex = 0;
        }
        if (desIndex < 0) {
            desIndex = numberCardRemains - 1;
        } else if (desIndex == numberCardRemains) {
            desIndex = 0;
        }
        Card tg = cardInHands[srcIndex];
        cardInHands[srcIndex] = cardInHands[desIndex];
        cardInHands[desIndex] = tg;
        int temp = cardInHands[srcIndex].getX();
        cardInHands[srcIndex].setX(cardInHands[desIndex].getX());
        cardInHands[desIndex].setX(temp);

    }

    public void doKeyRight() {
        if (isViewer) {
            return;
        }
        boolean isSwapCard = false;
        if (handCurseurIndex >= numberCardRemains) {
            handCurseurIndex = 0;
        }
        if (cardInHands[handCurseurIndex].getState() == Card.STATE_LOW) {
            isSwapCard = true;
        }
        handCurseurIndex++;
        if (handCurseurIndex == numberCardRemains) {
            handCurseurIndex = 0;
        }
        if (isSwapCard) {
            swapCard(handCurseurIndex - 1, handCurseurIndex);
        }
        parent.repaint();
    }

    public void inputKeyCode(int key) {
        if (isShowSpeaker) {
            speakerBox.inputChar(key);
            parent.repaint();
            return;
        }
        if (!isViewer) {
            chatBox.inputChar(key);
            isShowChatBox = true;
            parent.repaint();
        }
    }

    protected void doKeyUpRemain() {
        if (isViewer || gameData.gameTypePlay == Constants.BA_CAY) {
            return;
        }
        if (handCurseurIndex < 0) {
            return;
        }
        cardInHands[handCurseurIndex].changeState(Card.UP);
        parent.repaint();
    }

    protected void doKeyDownRemain() {
        if (handCurseurIndex < 0 || isViewer || gameData.gameTypePlay == Constants.BA_CAY) {
            return;
        }
        cardInHands[handCurseurIndex].changeState(Card.DOWN);
        parent.repaint();
    }

    protected void doSoftKeyLeftRemain() {
        if (isShowChatBox) {
            isShowChatBox = false;
            chatBox.setText("");
            parent.repaint();
            return;
        }

        doGameSoftKeyLeft();
    }

    protected abstract void doGameSoftKeyLeft();

    protected void doSoftKeyRightRemain() {
        if (isShowChatBox && !isViewer) {
            chatBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        doGameSoftKeyRight();
    }

    protected abstract void doGameSoftKeyRight();

    protected void reponseQuestion(int result) {
    }

    protected void touchPressedRemain(int x, int y) {
        pointerPressX = x;
        poiterPressY = y;
        isPointerDrag = false;
    }

    public void doTouchReleased(int x, int y) {
        Debug.d("is drag :" + isPointerDrag);
        if (isPointerDrag) {
            int deltaX = Math.abs(x - pointerPressX);
            int deltaY = Math.abs(y - poiterPressY);
            if (deltaX > deltaY) {
                if (x - pointerPressX < 0) {
                    Debug.d("move left");
                    doKeyLeft();
                    return;
                }
                if (x - pointerPressX > 0) {
                    doKeyRight();
                    Debug.d("move right");
                    return;
                }
            }

            if (y - poiterPressY < 0) {
                doKeyUp();
                Debug.d("move up");
                return;
            }
            if (y - poiterPressY > 0) {
                doKeyDown();
                Debug.d("move down");
                return;

            }
        }
    }

    public void doTouchDraged(int x, int y) {
        //  Debug.d("pointer drag");
        isPointerDrag = true;
        if (pointerPressX - 8 < x && x < pointerPressX + 8) {
            isPointerDrag = false;
        } else if (poiterPressY - 8 < y && y < poiterPressY + 8) {
            isPointerDrag = false;
        }

    }

    public void setPlayerId(int id) {
        this.playerId = id;
    }

    public void setNumberPlayer(int number) {
        Debug.d("set number players :" + number);
        this.numberPlayers = number;
    }

    public void setTableHostId(int id) {
//        this.tableHostId = id;
//        setupSubMenu();
    }

    public void setNumberPlayerReady(int number) {
    }

    public void setMaxPlayer(int number) {
        this.maxPlayer = number;
    }

    public void setGameRule(int gameRule) {
//        this.gameRule = gameRule;
//        parent.repaint();
    }

    public void setFirstTurn(int turn) {
        firstTurn = turn;
    }

    public void setMyViewId(int id) {
        myViewId = id;
        isViewer = (myViewId < maxPlayer ? false : true);
        if (isViewer) {
            playerId = 0;
        }
        Debug.d("set view id :" + myViewId + "," + isViewer);
        parent.repaint();
    }

    public void doKeyPound() {
    }
}
